<!DOCTYPE html>
<html>
<head>
    <title>电话号码更新测试</title>
    <meta charset="UTF-8">
</head>
<body>
    <h1>电话号码更新测试</h1>
    
    <div>
        <h3>当前用户信息</h3>
        <div id="currentInfo">加载中...</div>
    </div>
    
    <div>
        <h3>更新电话号码</h3>
        <input type="text" id="phoneNumber" placeholder="输入新的电话号码" value="13800138000">
        <button onclick="updatePhone()">更新电话号码</button>
    </div>
    
    <div>
        <h3>测试日志</h3>
        <div id="log" style="background: #f0f0f0; padding: 10px; height: 300px; overflow-y: scroll;"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8080';
        let token = '';

        function log(message) {
            const logDiv = document.getElementById('log');
            const time = new Date().toLocaleTimeString();
            logDiv.innerHTML += `[${time}] ${message}<br>`;
            logDiv.scrollTop = logDiv.scrollHeight;
            console.log(message);
        }

        // 获取JWT令牌（需要先登录）
        async function login() {
            try {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: 'sbxnh',  // 替换为您的用户名
                        password: '123456'   // 替换为您的密码
                    })
                });
                
                if (response.ok) {
                    const data = await response.json();
                    token = data.token;
                    log('登录成功，获取到token');
                    getCurrentInfo();
                } else {
                    log('登录失败: ' + response.status);
                }
            } catch (error) {
                log('登录错误: ' + error.message);
            }
        }

        // 获取当前用户信息
        async function getCurrentInfo() {
            try {
                const response = await fetch(`${API_BASE}/user/profile`, {
                    headers: {
                        'Authorization': `Bearer ${token}`,
                        'Content-Type': 'application/json'
                    }
                });
                
                if (response.ok) {
                    const data = await response.json();
                    document.getElementById('currentInfo').innerHTML = `
                        <p>用户名: ${data.username}</p>
                        <p>姓名: ${data.fullName}</p>
                        <p>邮箱: ${data.email}</p>
                        <p>电话: ${data.phoneNumber || '空'}</p>
                        <p>部门: ${data.department || '空'}</p>
                        <p>状态: ${data.status}</p>
                    `;
                    log('获取用户信息成功');
                } else {
                    log('获取用户信息失败: ' + response.status);
                }
            } catch (error) {
                log('获取用户信息错误: ' + error.message);
            }
        }

        // 更新电话号码
        async function updatePhone() {
            const phoneNumber = document.getElementById('phoneNumber').value;
            log(`准备更新电话号码为: ${phoneNumber}`);
            
            const requestData = {
                phoneNumber: phoneNumber
            };
            
            log(`发送的数据: ${JSON.stringify(requestData)}`);
            
            try {
                const response = await fetch(`${API_BASE}/user/profile`, {
                    method: 'PUT',
                    headers: {
                        'Authorization': `Bearer ${token}`,
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(requestData)
                });
                
                if (response.ok) {
                    const data = await response.json();
                    log('更新成功，返回数据: ' + JSON.stringify(data));
                    getCurrentInfo(); // 重新获取用户信息
                } else {
                    const errorText = await response.text();
                    log('更新失败: ' + response.status + ' - ' + errorText);
                }
            } catch (error) {
                log('更新错误: ' + error.message);
            }
        }

        // 页面加载时自动登录
        window.onload = function() {
            log('页面加载完成，开始测试...');
            login();
        };
    </script>
</body>
</html>
